<template>
  <div class="add">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 步骤条案 -->
    <el-steps :active="activeName-0"
              finish-status="success">
      <el-step title="步骤 1"></el-step>
      <el-step title="步骤 2"></el-step>
      <el-step title="步骤 3"></el-step>
      <el-step title="步骤 4"></el-step>
      <el-step title="步骤 5"></el-step>
    </el-steps>
    <!-- tabs -->
  <el-tabs v-model="activeName" tab-position="left" style="margin-top: 15px" >
    <el-tab-pane label="基本信息" name="0">
      <el-form ref="addForm" :model="addForm" label-width="80px">
  <el-form-item label="商品名称">
    <el-input v-model="addForm.goods_name"></el-input>
  </el-form-item>
    <el-form-item label="商品价格">
    <el-input v-model="addForm.goods_price"></el-input>
  </el-form-item>
      <el-form-item label="商品重量">
    <el-input v-model="addForm.goods_weight"></el-input>
  </el-form-item>
      <el-form-item label="商品数量">
    <el-input v-model="addForm.goods_number"></el-input>
  </el-form-item>
<!-- 级联选择器 -->
      <el-form-item label="商品分类">
      <el-cascader
      @change="changelabel"
      v-model="addForm.goods_cat"
    :options="goodsCategoriesList"
    :props="categeriesProp"
    clearable></el-cascader>
  </el-form-item>
</el-form>
    </el-tab-pane>
    <el-tab-pane label="商品参数" name="1">商品参数</el-tab-pane>
    <el-tab-pane label="商品属性" name="2">商品参数</el-tab-pane>
    <el-tab-pane label="商品图片" name="3">
     <el-upload
  class="upload-demo"
  action="http://localhost:8888/api/private/v1/upload"
  :headers="getToken()"
  :before-upload="beforeUpload"
  :on-remove="handleRemove"
  :on-success="handleSuccess"
  :file-list="fileList"
  list-type="picture">
  <el-button size="small" type="primary">点击上传</el-button>
  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
</el-upload>
    </el-tab-pane>
    <el-tab-pane label="商品内容" name="4">
        <quill-editor
          v-model="addForm.goods_introduce"
          ref="myQuillEditor"
          :options="editorOption"
          style="height: 200px;margin-bottom: 10px;border: 1px solid red;"
          >
        </quill-editor>
    </el-tab-pane>
  </el-tabs>
        <el-button type="primary"
        style="margin-left: 5%"
        @click="addCatagoriesBtn"
                 >添加商品</el-button>
  </div>
</template>

<script>
import { getCategories, addCategoriesGoods } from '@/api/goods.js'
export default {
  data () {
    return {
      editorOption: {},
      fileList: [],
      categeriesProp: {
        checkStrictly: true,
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      goodsCategoriesList: [],
      active: 0,
      activeName: '',
      addForm: {
        goods_name: '',
        goods_price: '',
        goods_weight: '',
        goods_number: '',
        goods_cat: '',
        pics: [],
        goods_introduce: '',
        goods_state: 1
      }
    }
  },
  mounted () {
    getCategories(3)
      .then((res) => {
        console.log(res)
        if (res.data.meta.status === 200) {
          this.goodsCategoriesList = res.data.data
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },
  methods: {
    getToken () {
      let token = localStorage.getItem('token')
      return {
        Authorization: token
      }
    },
    changelabel (value) {
      console.log(value)
      this.addForm.goods_cat = value.join(',')
    },
    handleRemove (file, fileList) {
      // console.log(file)
      if (!file.response) {
        return false
      }
      let removeEle = file.response.data.tmp_path
      for (let i = 0; i < this.addForm.pics.length; i++) {
        if (this.addForm.pics[i].pic === removeEle) {
          this.addForm.pics.splice(i, 1)
        }
      }
    },
    handleSuccess (response) {
      // console.log(response)
      this.addForm.pics.push([{ pic: response.data.tmp_path }])
    },
    // 添加商品
    addCatagoriesBtn () {
      console.log(this.addForm)
      addCategoriesGoods(this.addForm)
        .then((res) => {
          console.log(res)
          if (res.data.meta.status === 201) {
            this.$message({
              type: 'seccuss',
              message: res.data.meta.msg
            })
            this.$router.push({ name: 'list' })
          } else {
            this.$message({
              type: 'info',
              message: res.data.meta.msg
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    beforeUpload (file) {
      console.log(file)
      if (file.type.indexOf('image/') === -1) {
        this.$message({
          type: 'info',
          message: '文件格式有误'
        })
        return false
      }
    }
  }

}
</script>

<style lang="less" scoped>
  .add{
    padding-bottom: 20px;
  }
</style>
